<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/8/3
 * Time: 上午5:55
 */

namespace app\wechat\controller;


use app\wechat\handler\EventHandler;
use app\wechat\handler\TextHandler;
use EasyWeChat\Kernel\Messages\Message;
use EasyWeChat\Kernel\Messages\Transfer;
use extend\Functions;
use extend\STATUS_CODE;
use Naixiaoxin\ThinkWechat\Facade;
use think\Request;

class Index extends Base
{
    /**
     * @purpose 设置微信公众号菜单
     * @return array
     */
    public function setMenu () {
        $app        = Facade::officialAccount();
        $button     = [
            [
                'type' => 'view',
                'name' => '活动报名',
                'url'  => rtrim(config('app.client_domain'), '/').'/activity'
            ],
            [
                'type' => 'view',
                'name' => '个人中心',
                'url'  => rtrim(config('app.client_domain'), '/').'/mine'
            ]
        ];
        $response = $app->menu->create($button);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $response
        );
    }
    /**
     * @purpose 接收微信消息
     * @throws \EasyWeChat\Kernel\Exceptions\BadRequestException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \ReflectionException
     */
    public function api ()
    {
        $app = Facade::officialAccount();
        // 处理公众号文本消息
        $app->server->push(TextHandler::class, Message::TEXT);
        // 处理公众号事件消息
        $app->server->push(EventHandler::class, Message::EVENT);
        $app->server->push(function($message) {
            return new Transfer();
        });
        $response = $app->server->serve();
        $response->send();
    }
    /**
     * @purpose 获取jssdk 签名
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function jsSdk (Request $request) {
        $url = $request->get('url', config('client_domain'));
        $app = Facade::officialAccount();
        $api = ['onMenuShareTimeline',
            'onMenuShareAppMessage',
            'onMenuShareQQ',
            'onMenuShareWeibo',
            'onMenuShareQZone',
            'startRecord',
            'stopRecord',
            'onVoiceRecordEnd',
            'playVoice',
            'pauseVoice',
            'stopVoice',
            'onVoicePlayEnd',
            'uploadVoice',
            'downloadVoice',
            'chooseImage',
            'previewImage',
            'uploadImage',
            'downloadImage',
            'translateVoice',
            'getNetworkType',
            'openLocation',
            'getLocation',
            'hideOptionMenu',
            'showOptionMenu',
            'hideMenuItems',
            'showMenuItems',
            'hideAllNonBaseMenuItem',
            'showAllNonBaseMenuItem',
            'closeWindow',
            'scanQRCode',
            'chooseWXPay',
            'openProductSpecificView',
            'addCard',
            'chooseCard',
            'scanQRCode',
            'openCard'];
        $app->jssdk->setUrl($url);
        $sign = $app->jssdk->buildConfig($api, env('app_debug'), false,false);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $sign
        );
    }
}